if lib and not lib.CalcRisk then


function lib:CalcRisk(instrument, price, stop, lots)
	if type(instrument) == "table" and price and stop and price ~= stop then
		local max_lots, commission = self:GetMaxVolume(instrument, price > stop, price)
		local assets = self:GetAssets()
		
		if max_lots and commission and assets then
			--local spread = self:GetSpread(instrument, lots or max_lots, price < stop)
			--if spread then
			--	commission = commission + spread
			--end
			
			commission = commission + instrument.min_price_step / price
			
			local risk = math.abs(price - stop)
					* (1 + commission)
					* (lots or max_lots)
					* self:GetLotSize(instrument)
					* 100 / self:GetAssets()
					
			return risk, max_lots, commission
		end
	end
end


function lib:GetMaxVolume(instrument, is_long, price)
	price = price or 0
	
	local qty, commission  = CalcBuySell(
		instrument.class_code,
		instrument.code,
		self:GetClientCode(),
		self:GetAccount(instrument.class_code),
		price,
		is_long,
		price <= 0
	)
	message("GetMaxVolume " .. tostring(self:GetClientCode()) .. " " .. tostring(self:GetAccount(instrument.class_code)) .. " " .. tostring(instrument.code) .. " " .. tostring(qty) .. " " .. tostring(commission))
	
	qty = qty or 0
	commission = commission or 0
	
	if qty > 0 then
		if commission <= 0 then
			--commission = qty * (1.0 + (self:getParamEx(instrument, "stepprice") or 1.0))
			commission = 0
		end
		commission = commission * 2
		if price > 0 then
			commission = commission / price / qty / self:GetLotSize(instrument)
		end
		
		if qty >= 4 then
			qty = qty - 1
		--else
			--qty = 0
		end
	end
	
	return qty, commission
end


end -- of file
